package cn.lili.modules.order.order.callback;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.vo.ResponseBean;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.vos.yunxin.CallBackVO;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.order.aftersale.entity.dos.AfterSale;
import cn.lili.modules.order.aftersale.service.AfterSaleService;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import cn.lili.modules.order.order.service.OrderItemService;
import cn.lili.modules.order.order.service.OrderService;
import cn.lili.modules.order.trade.entity.enums.AfterSaleRefundWayEnum;
import cn.lili.modules.order.trade.entity.enums.AfterSaleStatusEnum;
import cn.lili.modules.order.trade.entity.enums.AfterSaleTypeEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
 * 售后审核成功
 */
@Service("after_sales.pass")
@Slf4j
public class AfterSalesPassService implements MessageType{
    @Lazy
    @Autowired
    private OrderItemService orderItemService;
    @Lazy
    @Autowired
    private AfterSaleService afterSaleService;

    @Override
    public ResponseBean execute(CallBackVO callBackVO) {
        OrderItem orderItem = orderItemService.getOne(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getSupplyChainOrderId, callBackVO.getOrder_item_id()).last("limit 1"));
        log.info("orderItem == {} ", JSONObject.toJSONString(orderItem));
        if (orderItem == null) {
            return null;
        }
        AfterSale afterSale = afterSaleService.getOne(new LambdaQueryWrapper<AfterSale>().eq(AfterSale::getOrderItemSn, orderItem.getSn()).orderByDesc(AfterSale::getCreateTime).last("limit 1"));
        if (afterSale == null) {
            return null;
        }
//        afterSaleService.review(afterSale.getSn(), "PASS", "壹邦玖供应链已审核通过");
        //判断为待审核的售后服务
        if (!afterSale.getServiceStatus().equals(AfterSaleStatusEnum.APPLY.name())) {
            throw new ServiceException(ResultCode.AFTER_SALES_DOUBLE_ERROR);
        }
        //强制退款金额=售后申请金额
        afterSale.setActualRefundPrice(afterSale.getApplyRefundPrice());
        //判断审核状态
        //如果售后类型为：退款，审核状态为已通过并且退款方式为原路退回，售后单状态为已完成。
        //如果售后类型为：退款，审核状态已通过并且退款方式为线下退回，售后单状态为待退款。
        //如果售后类型不为退款，售后单状态为：已通过。
        AfterSaleStatusEnum afterSaleStatusEnum;
        //如果为退款操作
        if (afterSale.getServiceType().equals(AfterSaleTypeEnum.RETURN_MONEY.name()) && afterSale.getRefundWay().equals(AfterSaleRefundWayEnum.ORIGINAL.name())) {
            //修改售后单状态为已完成
            afterSaleStatusEnum = AfterSaleStatusEnum.COMPLETE;
        }
        // 如果退货操作 && 线下支付 则售后单状态为待退款
        else if (afterSale.getServiceType().equals(AfterSaleTypeEnum.RETURN_MONEY.name())) {
            afterSaleStatusEnum = AfterSaleStatusEnum.WAIT_REFUND;
        }
        //否则为退货操作 审核通过，则标记为申请通过，由买家继续执行
        else {
            afterSaleStatusEnum = AfterSaleStatusEnum.PASS;
        }
        afterSale.setServiceStatus(afterSaleStatusEnum.name());
        afterSale.setAuditRemark("壹邦玖供应链已审核通过");
        //根据售后编号修改售后单
        afterSaleService.updateById(afterSale);
        //根据售后状态。修改OrderItem订单中正在售后商品数量及状态
        afterSaleService.updateOrderItemAfterSaleStatus(afterSale);
        //发送售后消息
        afterSaleService.sendAfterSaleMessage(afterSale);
        return new ResponseBean(0, "操作成功", new HashMap<>());
    }
}
